<template>
  <div class="login">
    <van-image class="hot_icon" :src="require('@/assets/img/projectLogo.jpg')" fit="cover" />
    <div class="btnList">
      <van-button type="primary" round class="loginBtn" color="#FF7733" @click="getWxUserInfo()">微信登陆</van-button>
      <van-button round class="phone" color="#F2F2F2" @click="getWxUserInfo(true)">手机号登陆</van-button>
    </div>
  </div>
</template>
<script>
import { Toast } from 'vant';
export default {
  name: '',
  components: {

  },
  mixins: [],
  props: {

  },
  data () {
    return {

    }
  },
  computed: {

  },
  watch: {

  },
  created () {
    if (this.$route.query.from_uid) {
      sessionStorage.setItem('from_uid', this.$route.query.from_uid)
    }
  },
  mounted () {
    this.getWxCode()
  },
  methods: {
    //获取微信授权URL
    getWxUserInfo (flag) {
      let url = flag ? window.location.origin + '/view/MobileLogin' : window.location.href

      this.$axios.post(this.$api.Authorize, {
        url: url
      }).then(res => {

        //this.getWxCode(res.data.data.url)
        window.location.href = res.data.data.url
      })

    },
    //请求个人信息数据
    getUserInfo () {

      this.$axios
        .get(this.$api.GetUserAllInfo)
        .then((res) => {

          if (res.data.data.data) {
            this.$store.commit('setUserInfo', res.data.data.data)
            this.userInfo = this.$store.state.userInfo
          }


        });
    },
    // 微信授权获取用户信息
    getWxCode () {
      let code = this.getUrlCode().code;
      let from_uid = sessionStorage.getItem('from_uid')
      //如果没有code 去获取code
      if (code) {
        this.$axios.post(this.$api.Authlogin, {
          code: code,
          state: 'STATE',
          from_uid: from_uid || null
        }).then(res => {

          if (res.data.code != 1) {
            Toast.fail('登录失败')
          } else {
            this.$store.commit('settoken', res.data.data.token);
            // router.go(0);
            this.getUserInfo()
            localStorage.setItem("updatePage", 1)
            let share = JSON.parse(sessionStorage.getItem("share"))
            console.log(share);
            if (share) {
              this.$router.replace({
                name: share.name,
                query: {
                  id: share.id
                }

              })
            } else {
              this.$router.replace('./')
            }

            //sessionStorage.removeItem('url')

          }
        })
      }
    },

    //截取code
    getUrlCode () { // 截取url中的code方法
      var url = location.search
      var theRequest = new Object()
      if (url.indexOf("?") != -1) {
        var str = url.substr(1)
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
        }
      }
      return theRequest
    }
  }
};
</script>
<style lang='less' scoped>
.login {
  min-height: 100vh;
  background: white;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.van-image {
  margin-top: 30px;
  width: 190px;
  height: 190px;
}
.btnList {
  display: flex;
  flex-direction: column;
  margin-top: 40px;
  .van-button {
    width: 300px;
    margin-bottom: 12px;
  }
  .phone {
    font-size: 14px;

    color: rgba(32, 35, 39, 0.4) !important;
  }
}
</style>